FLOPPY_IMAGE := a.img
HD_IMAGE := hd.img
TARGET := system
OBJECTS := boot/setup.o \
		   main.o \
		   drivers/vga.o drivers/keyboard.o drivers/printk.o\
		   arch/interrupt.o  arch/tss.o arch/page.o\
		   kernel/memory.o

CC := gcc

all:
	echo "use make floppy or make hd"

CFLAGS := -c -O0 -Wall -nostdinc -fno-builtin -fno-stack-protector -funsigned-char \
		 -finline-functions -finline-small-functions -findirect-inlining \
		 -finline-functions-called-once  -m32 -ggdb -gstabs+ -fdump-rtl-expand

CFLAGS += -Idrivers -Iarch

floppy:boot/bootsect.bin $(OBJECTS) kernel.elf
	#dd if=bootsect.bin $(TARGET) of=a.img bs=512 count=2880
	dd if=boot/bootsect.bin of=$(FLOPPY_IMAGE) bs=512 count=1 conv=notrunc
	dd if=$(TARGET) of=$(FLOPPY_IMAGE) bs=512 count=2879 seek=1 conv=notrunc

hd:boot/bootsect.bin $(OBJECTS) kernel.elf
	dd if=boot/bootsect.bin of=$(HD_IMAGE) bs=512 count=1 conv=notrunc
	dd if=$(TARGET) of=$(HD_IMAGE) bs=512 count=2879 seek=1 conv=notrunc

boot/bootsect.bin:boot/bootsect.asm
	nasm -fbin $< -l boot/bootsect.s -o $@

boot/setup.o:boot/setup.asm
	nasm -f elf32 -g -F stabs -l boot/setup.s $^ -o $@ 

main.o:main.c
	$(CC) $(CFLAGS) -c $< -o $@ 

drivers/vga.o:drivers/vga.c drivers/vga.h
	$(CC) $(CFLAGS) -c $<  -o $@

 



arch/interrupt.o:arch/interrupt.c arch/interrupt.h
	$(CC) $(CFLAGS) -c $<  -o $@

kernel.elf: $(OBJECTS) 
	ld -Tlink.ld -melf_i386 -static -o kernel.elf $(OBJECTS) -M> map.map
	objcopy -O binary kernel.elf $(TARGET)	 

clean:
	-rm boot/*.bin boot/*.s boot/*.o map.map *.elf \
	$(TARGET)  $(IMAGE) \
    drivers/*.o  arch/*.o *.o








